package cn.ante.ymcc.service.impl;

import cn.ante.ymcc.constance.CommonExceptionEnum;
import cn.ante.ymcc.domain.MessageSms;
import cn.ante.ymcc.domain.MessageSmsPullblack;
import cn.ante.ymcc.mapper.MessageSmsMapper;
import cn.ante.ymcc.mapper.MessageSmsPullblackMapper;
import cn.ante.ymcc.service.IMessageSmsService;
import cn.ante.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Nai
 * @since 2022-08-06
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {
    @Autowired
    private MessageSmsMapper messageSmsMapper;

    @Autowired
    private MessageSmsPullblackMapper messageSmsPullblackMapper;
    @Override
    public void reject(Long id) {
        // 根据前端拿到的id拿到对应的数据库中的短信消息
        MessageSms messageSms = messageSmsMapper.selectById(id);
        // 拿到短息消息对象中的电话，与黑名单中的对比，如果该手机号已经存在，就不再发送短信和，也不再将其拉入黑名单
        List<MessageSmsPullblack> messageSmsPullblacks = messageSmsPullblackMapper.selectList(null);
        Map<String, MessageSmsPullblack> pullblackHashMap = new HashMap<>();
        for (MessageSmsPullblack mb: messageSmsPullblacks) {
            pullblackHashMap.put(mb.getPhone(),mb);
        }
        MessageSmsPullblack pullblack = pullblackHashMap.get(messageSms.getPhone());
        AssertUtil.isNull(pullblack, CommonExceptionEnum.MESSAGE_PULL_BLACK.getCode(),
                CommonExceptionEnum.MESSAGE_PULL_BLACK.getMessage());
        MessageSmsPullblack messageSmsPullblack = new MessageSmsPullblack();
        // 将短信消息对象中的字段分别设置黑名单中
        messageSmsPullblack.setPhone(messageSms.getPhone());
        messageSmsPullblack.setIp(messageSms.getIp());
        messageSmsPullblack.setSendTime(new Date());
        messageSmsPullblack.setUserId(messageSmsPullblack.getUserId());
        messageSmsPullblackMapper.insert(messageSmsPullblack);
    }
}
